package com.illtamer.infinite.collapsar.server.handler;

import io.netty.channel.Channel;

import java.util.Locale;

public abstract class AbstractCommandHandler implements Handler {

    @Override
    public void handle(String message, Channel channel) {
        String command;
        String content;
        int emptyIndex = message.indexOf(' ');
        if (emptyIndex == -1) {
            command = message.toUpperCase(Locale.ROOT);
            content = null;
        } else {
            if (emptyIndex == 0) {
                channel.writeAndFlush("500\r\n");
                return;
            }
            command = message.substring(0, emptyIndex).toUpperCase(Locale.ROOT);
            content = message.substring(emptyIndex + 1);
        }
        onCommand(command, content, channel);
    }

    abstract public void onCommand(String command, String content, Channel channel);

}
